//headers 
#include<algorithm>
#include<cassert>
#include<cstdio>
#include<deque>
#include<iostream>
#include<list>
#include<map>
#include<numeric>
#include<deque>
#include<set>
#include<string>
#include<sstream>
#include<stack>
#include<streambuf>
#include<vector>
using namespace std;

//macros
#define LET(x,a) typeof(a) x(a)
#define FOR(i,a,n) for(LET(i,a);i!=n;i++)
#define REP(i,n) FOR(i,0,n)
#define sz size()
#define pb push_back
#define ALL(x) x.begin(),x.end()
#define dbge(x) cout<<#x<<"->"<<x<<endl;
#define PRINT(x) REP(_,x.sz) cout<<x[_]<<" "; cout<<endl;
#define OUT(x) x<<" "
const int INF = (int) 1e8;
#define VI vector<int>
#define VS vector<string>
#define VVI vector< vector<int> >
#define LL long long

//code of suren


VI seen;
int fi;

struct pp{
  int node;
  int fi;
  
  pp(int a , int b) { node = a; fi = b; }
 
};

bool cmp ( const struct pp &a, const struct pp &b) {
  return a.fi > b.fi;
}

void dfs(VVI &grid,int node){
  seen[node]=1;
  REP(i,grid[node].sz) { fi++; if(!seen[ grid[node][i] ])  dfs(grid,grid[node][i]); }
  return;
}

int main(){
  int times; cin>>times;
  VVI grid,grid2;
  while(times--){
    int n; cin>>n;  int ans = 0;
    grid.clear(); grid.resize(n);
    grid2.clear(); grid2.resize(n);
    seen.clear(); seen.resize(n,0);
    int m; int a,b;
    cin>>m; vector<struct pp> dom;
    REP(i,m){ cin>>a>>b; grid[a-1].pb(b-1); grid2[b-1].pb(a-1);} 
    REP(i,n)  if( !grid2[i].sz ) { dfs(grid,i); ans++; }
    REP(i,n) if(!seen[i]){  dfs(grid,i); ans++; }
      
    cout<<ans<<endl;
  }
  return 0;
}
